/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#pragma once

#include "io/serverconnection.h"
#if defined Q_OS_LINUX
    #include <pthread.h>
    #include <sys/socket.h>
#define SOCKET int
#elif defined Q_OS_WIN32
    #include <windows.h>
    #include <winsock.h>
#elif defined Q_OS_WINCE
    #include <windows.h>
    #include <winsock.h>
#endif

#include "netdll.h"

namespace greenhills {
namespace net {

    /**
     * @brief This class contains implementation of message server socket that
     * can be bound to specific port and listen for incoming connections.
     *
     * @author Alexander Nikolskiy (alexander.nikolskiy@tieto.com)
     */
    class GH_NET_CLASS CMsgServerSocketConnection : public io::CServerConnection
    {
    public:
        /**
         * @brief Default constructor.
         */
        CMsgServerSocketConnection();

        /**
         * @brief Destructor.
         */
        virtual ~CMsgServerSocketConnection();

    public:
        /**
         * @brief Starts listening to incoming connections.
         *
         * @param pPort Port to listen on.
         */
        void listen(unsigned short pPort);

        /**
         * @brief Exits from listening mode.
         */
        void stopListening();

        /**
         * @brief Returns true if the server socket is in listening mode,
         * false otherwise.
         *
         * @return true if the server socket is in listening mode,
         * false otherwise.
         */
        bool isListening();

        /**
         * @brief This method implements a loop in which accepting of new
         * connections occur.
         */
        void acceptNewConnections();

    private:
        /**
         * @brief Holds number of socket assigned to this connection.
         */
        SOCKET mSocket;

        /**
         * @brief Indicates whether the server socket is in mode to accept new
         * connections or not.
         */
        bool mListening;

        /**
         * @brief ID of a thread in which accept loop is being executed.
         */
#if defined Q_OS_LINUX
        pthread_t mAcceptThread;
#elif defined Q_OS_WIN32
        HANDLE mAcceptThread;
#elif defined Q_OS_WINCE
        HANDLE mAcceptThread;
#endif

    };

} //namespace net
} //namespace greenhills
